import { accessLogMock } from './data/access'
import { isDateTimeInRange } from '../../utils'
let accessLogList = accessLogMock(400)
export default [
  {
    url: RegExp('/infra/api-access-log/page' + '.*'), // 查询分页列表
    method: 'get',
    response: params => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getListByPage(params)
      }
    }
  },
  {
    url: RegExp('/infra/api-access-log/export' + '.*'), // 导出
    method: 'get',
    response: params => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getListByExport(params)
      }
    }
  }
]

const getListByPage = params => {
  let result = accessLogList
  result = result.filter(item => !item.deleted)
  if (params.query.userId) {
    result = result.filter(item => item.userId === parseInt(params.query.userId))
  }
  if (params.query.userType) {
    result = result.filter(item => item.userType === parseInt(params.query.userType))
  }
  if (params.query.applicationName) {
    result = result.filter(item => item.applicationName.indexOf(params.query.applicationName) !== -1)
  }
  if (params.query.duration) {
    result = result.filter(item => item.duration === parseInt(params.query.duration))
  }
  if (params.query.resultCode) {
    result = result.filter(item => item.resultCode === parseInt(params.query.resultCode))
  }
  if (params.query.beginTime && params.query.beginTime.length !== 0) {
    result = result.filter(item => isDateTimeInRange(params.query.beginTime[0], params.query.beginTime[1], item.beginTime))
  }
  return {
    list: result.slice(parseInt(params.query.pageSize) * (parseInt(params.query.pageIndex) - 1), parseInt(params.query.pageSize) * (parseInt(params.query.pageIndex) - 1) + parseInt(params.query.pageSize)),
    total: result.length
  }
}

const getListByExport = params => {
  let result = accessLogList
  result = result.filter(item => !item.deleted)
  if (params.query.userId) {
    result = result.filter(item => item.userId === parseInt(params.query.userId))
  }
  if (params.query.userType) {
    result = result.filter(item => item.userType === parseInt(params.query.userType))
  }
  if (params.query.applicationName) {
    result = result.filter(item => item.applicationName.indexOf(params.query.applicationName) !== -1)
  }
  if (params.query.duration) {
    result = result.filter(item => item.duration === parseInt(params.query.duration))
  }
  if (params.query.resultCode) {
    result = result.filter(item => item.resultCode === parseInt(params.query.resultCode))
  }
  if (params.query.beginTime && params.query.beginTime.length !== 0) {
    result = result.filter(item => isDateTimeInRange(params.query.beginTime[0], params.query.beginTime[1], item.beginTime))
  }
  return result
}
